package com.ysg.sportsportlsit.service.impl;

import com.ysg.common.exception.ServiceException;
import com.ysg.common.utils.DateUtils;
import com.ysg.common.utils.StringUtils;
import com.ysg.sportsportlsit.domain.SportDeclareInformation;
import com.ysg.sportsportlsit.mapper.SportDeclareInformationMapper;
import com.ysg.sportsportlsit.service.ISportDeclareInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 学生报名信息Service业务层处理
 * 
 * @author 叶双贵
 * @date 2022-03-25
 */
@Service
public class SportDeclareInformationServiceImpl implements ISportDeclareInformationService
{
    @Autowired
    private SportDeclareInformationMapper sportDeclareInformationMapper;


    /**
     * 导入数据
     *
     * @param dataList 数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importOrderPlan(List<SportDeclareInformation> dataList, boolean updateSupport, String operName) {
        if(dataList==null||dataList.size()==0){
            throw new ServiceException("导入数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SportDeclareInformation data : dataList)
        {
            try
            {
                // 验证是否存在这个数据编号
                SportDeclareInformation m = sportDeclareInformationMapper.selectSportDeclareInformationByDeclareUid(data.getDeclareUid());
                if (StringUtils.isNull(m))
                {
                    data.setCreateBy(operName);
                    this.insertSportDeclareInformation(data);
                    successNum++;
                    successMsg.append( successNum + "、学生学号 " + data.getSportNameUser() + " 导入成功");
                }
                else if (updateSupport)
                {
                    data.setUpdateBy(operName);
                    this.updateSportDeclareInformation(data);
                    successNum++;
                    successMsg.append( successNum + "、学生学号 " + data.getSportNameUser() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append( failureNum + "、学生学号 " + data.getSportNameUser() + " 已存在");
                }
            }
            catch (Exception e)
            {
                if(data==null){ throw new ServiceException("学生学号为空！"); }
                failureNum++;
                String msg =  failureNum + "、学生学号 " + data.getSportNameUser() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



    // 查询学生报名信息条数
    @Override
    public int boo_bm_xm_TF(String sportlist_id,String sport_name_user) {
        return sportDeclareInformationMapper.boo_bm_xm_TF(sportlist_id,sport_name_user);
    }

     //查询学生报名信息
    @Override
    public List<SportDeclareInformation> boo_bm_xm_shuju(String sport_name_user) {
        return sportDeclareInformationMapper.boo_bm_xm_shuju(sport_name_user);
    }

    //管理员查询学生报名数据的集合
    @Override
    public List<SportDeclareInformation> admin_bm_xm_shuju() {
        return sportDeclareInformationMapper.admin_bm_xm_shuju();
    }

    /**
     * 查询学生报名信息
     * 
     * @param declareUid 学生报名信息主键
     * @return 学生报名信息
     */
    @Override
    public SportDeclareInformation selectSportDeclareInformationByDeclareUid(String declareUid)
    {
        return sportDeclareInformationMapper.selectSportDeclareInformationByDeclareUid(declareUid);
    }

    /**
     * 查询学生报名信息列表
     * 
     * @param sportDeclareInformation 学生报名信息
     * @return 学生报名信息
     */
    @Override
    public List<SportDeclareInformation> selectSportDeclareInformationList(SportDeclareInformation sportDeclareInformation)
    {
        return sportDeclareInformationMapper.selectSportDeclareInformationList(sportDeclareInformation);
    }

    /**
     * 新增学生报名信息
     * 
     * @param sportDeclareInformation 学生报名信息
     * @return 结果
     */
    @Override
    public int insertSportDeclareInformation(SportDeclareInformation sportDeclareInformation)
    {
        sportDeclareInformation.setCreateTime(DateUtils.getNowDate());
        return sportDeclareInformationMapper.insertSportDeclareInformation(sportDeclareInformation);
    }

    /**
     * 修改学生报名信息
     * 
     * @param sportDeclareInformation 学生报名信息
     * @return 结果
     */
    @Override
    public int updateSportDeclareInformation(SportDeclareInformation sportDeclareInformation)
    {
        sportDeclareInformation.setUpdateTime(DateUtils.getNowDate());
        return sportDeclareInformationMapper.updateSportDeclareInformation(sportDeclareInformation);
    }

    /**
     * 批量删除学生报名信息
     * 
     * @param declareUids 需要删除的学生报名信息主键
     * @return 结果
     */
    @Override
    public int deleteSportDeclareInformationByDeclareUids(String[] declareUids)
    {
        return sportDeclareInformationMapper.deleteSportDeclareInformationByDeclareUids(declareUids);
    }

    /**
     * 删除学生报名信息信息
     * 
     * @param declareUid 学生报名信息主键
     * @return 结果
     */
    @Override
    public int deleteSportDeclareInformationByDeclareUid(String declareUid)
    {
        return sportDeclareInformationMapper.deleteSportDeclareInformationByDeclareUid(declareUid);
    }
}
